#include "receive.h"

ReceiveThread::ReceiveThread(SerialPortInterface::SerialPort *serialport, QObject *parent) :
    QThread(parent)
{
    this->serialport = serialport;
}

void ReceiveThread::run()
{
    char tmp;

    forever {
        tmp = serialport->getChar();
        CharReceived(tmp);
    }
}

Receive::Receive(SerialPortInterface::SerialPort *serialport, QObject *parent) :
    QObject(parent)
{
    this->serialport = serialport;
    this->receivethread = new ReceiveThread(serialport);

    connect(receivethread, SIGNAL(CharReceived(char)), this, SLOT(CharReceived(char)));

    receivethread->start();
}

Receive::~Receive()
{
    delete receivethread;
}

void Receive::CharReceived(char ch)
{
    QString buf;

    switch(displaytype)
    {
    case ascii:
        buf.push_back(ch);
        break;
    case asciihex:
        buf.push_back(ch);
        break;
    case uint8:
        buf = QString::number( (quint8)ch );
        break;
    case int8:
        buf = QString::number( (qint8)ch);
        break;
    case hex:
        break;
    case binary:
        break;
    }

    CharReceived(buf);
}

void Receive::Displayasciihex()
{
    displaytype = asciihex;
}

void Receive::Displaybinary()
{
    displaytype = binary;
}

void Receive::Displayhex()
{
    displaytype = hex;
}

void Receive::Displayint8()
{
    displaytype = int8;
}

void Receive::Displayascii()
{
    displaytype = ascii;
}

void Receive::Displayuint8()
{
    displaytype = uint8;
}
